Next | Prev | Up | Top | Contents | Index

Using the Archiver to Create Libraries

An archive library is a file that includes the contents of one or more object (.o) files. When the linker (ld) searches for a symbol in an archive library, it loads only the code from the object file where that symbol was defined (not the entire library) and links it with the calling program.

The archiver (ar) creates and maintains archive libraries and has the following main functions:

The following section explains the syntax of the ar command and lists some examples of how to use it. See the ar(1) reference page for details.

Note: ar simply strings together whatever object files you tell it to archive; thus, it can be used to build either non-shared or PIC libraries, depending on how the included .o files were built in the first place. If you do create a non-shared library with ar, remember to link it -non_shared with your other code. For information about building DSOs and converting libraries to DSOs, see Chapter 3.


ar Syntax

The syntax for ar is:

ar options [posObject] libName [object1...objectn]

options

Specifies the action that the archiver is to take. Table 2-12 and Table 2-13 list the available options. To specify more than one option, don't use a dash or put spaces between the options. For example, use ar ts, not ar -t -s.

posObject

Specifies the name of an object within an archive library. It specifies the relative placement (either before or after posObject) of an object that is to be copied into the library or moved within the library. This parameter is required when the a, b, or i suboptions are specified with the m or r option. The last example in "ar Examples," shows the use of a posObject parameter.

libName

Specifies the name of the archive library you are creating, updating, or extracting information from.

object

Specifies the name(s) of the object file(s) to manipulate.

ar Syntax
ar Options
ar Examples

Next | Prev | Up | Top | Contents | Index